<?php
abstract class BaseController {
	
    private $view = array();
    
	protected static $state;
    
	protected static $action;

	public function __construct($state, $action) {
        if (ROUTER == 1) {
            $paramsArr = explode('/', HttpParams::get('q'));
            $i = 0;
            if (is_array(RouterConfig::$config[Configure::$app][$state][$action])) {
                foreach (RouterConfig::$config[Configure::$app][$state][$action] as $v) {
                    if (!empty($paramsArr[$i])) {
                        HttpParams::set($v, $paramsArr[$i]);
                        Common::debug($v . ':' . $paramsArr[$i], 'HttpParams-set');
                    }
                    $i++;
                }
            }
        }
        self::$state = $state;
        self::$action = $action;
	}
    
	public function runCommand() {
		$action = BaseModelCommon::getFormatName(self::$action);
		if( method_exists($this, $action) ) {
			call_user_func_array(array(&$this, $action),array());
		} else {
            $action = htmlspecialchars($action);//防止XSS
			Message::showError("action : {$action}不存在", 'index.php');
		}
	}

    //*************************************************模版处理*********************************************************
    //设置模版变量
	protected function setView($key, $value) {
		$this->view[$key] = $value;
	}

    //显示模版
	protected function display($tpl_file) {
		$tpl = new BaseView();
		$tpl->assign($this->view);
		$str = microtime(true)-$_SERVER['startime'];
		$tpl->assign('str', $str);
		$tpl->display($tpl_file);
	}
}
?>
